﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FR.SAFTAnalyser.Model;

namespace FR.SAFTAnalyser.SAFT
{
    internal class ConvertV2ToV3
    {
        internal static AuditFile Convert(Model.V2.AuditFile oldFileVersion)
        {
            if (oldFileVersion == null)
                return null;

            AuditFile auditFile = new AuditFile();

            auditFile.Header = GetHeader(oldFileVersion.Header);
            auditFile.MasterFiles = GetMastesFiles(oldFileVersion.MasterFiles);
            auditFile.GeneralLedgerEntries = GetGeneralLedgerEntries(oldFileVersion.GeneralLedgerEntries);
            auditFile.SourceDocuments = GetSourceDocuments(oldFileVersion.SourceDocuments);

            return auditFile;
        }

        #region SourceDocuments
        private static SourceDocuments GetSourceDocuments(Model.V2.SourceDocuments sourceDocumentsOld)
        {
            if (sourceDocumentsOld == null)
                return null;

            SourceDocuments sourceDocuments = new SourceDocuments();
            sourceDocuments.SalesInvoices = GetSalesInvoices(sourceDocumentsOld.SalesInvoices);
            sourceDocuments.MovementOfGoods = GetMovementOfGoods(sourceDocumentsOld.MovementOfGoods);
            sourceDocuments.WorkingDocuments = GetWorkingDocuments(sourceDocumentsOld.WorkingDocuments);

            return sourceDocuments;
        }

        #region WorkingDocuments
        private static SourceDocumentsWorkingDocuments GetWorkingDocuments(Model.V2.SourceDocumentsWorkingDocuments workingDocumentsOld)
        {
            if (workingDocumentsOld == null)
                return null;

            SourceDocumentsWorkingDocuments workingDocuments = new SourceDocumentsWorkingDocuments();
            workingDocuments.NumberOfEntries = workingDocumentsOld.NumberOfEntries;
            workingDocuments.TotalCredit = workingDocumentsOld.TotalCredit;
            workingDocuments.TotalDebit = workingDocumentsOld.TotalDebit;
            workingDocuments.WorkDocument = GetWorkDocument(workingDocumentsOld.WorkDocument);

            return workingDocuments;
        }

        private static SourceDocumentsWorkingDocumentsWorkDocument[] GetWorkDocument(Model.V2.SourceDocumentsWorkingDocumentsWorkDocument[] workDocumentOld)
        {
            if (workDocumentOld == null || workDocumentOld.Length == 0)
                return null;

            List<SourceDocumentsWorkingDocumentsWorkDocument> workDocument = new List<SourceDocumentsWorkingDocumentsWorkDocument>();
            foreach (var docOld in workDocumentOld)
            {
                SourceDocumentsWorkingDocumentsWorkDocument doc = new SourceDocumentsWorkingDocumentsWorkDocument();
                doc.CustomerID = docOld.CustomerID;
                doc.DocumentNumber = docOld.DocumentNumber;
                doc.DocumentStatus = GetWorkingDocumentWorkStatus(docOld.DocumentStatus);
                doc.DocumentTotals = GetWorkingDocumentDocumentTotals(docOld.DocumentTotals);
                doc.EACCode = docOld.EACCode;
                doc.Hash = docOld.Hash;
                doc.HashControl = docOld.HashControl;
                doc.Line = GetWorkingDocumentLines(docOld.Line);
                doc.Period = docOld.Period;
                doc.SourceID = docOld.SourceID;
                doc.SystemEntryDate = docOld.SystemEntryDate;
                doc.WorkDate = docOld.WorkDate;
                doc.WorkType = EnumParser.Parse<WorkType>(docOld.WorkType.ToString());

                workDocument.Add(doc);
            }


            return workDocument.ToArray();
        }

        private static SourceDocumentsWorkingDocumentsWorkDocumentLine[] GetWorkingDocumentLines(Model.V2.SourceDocumentsWorkingDocumentsWorkDocumentLine[] linesOld)
        {
            List<SourceDocumentsWorkingDocumentsWorkDocumentLine> lines = new List<SourceDocumentsWorkingDocumentsWorkDocumentLine>();

            if (linesOld != null && linesOld.Length > 0)
            {
                foreach (var lineOld in linesOld)
                {
                    SourceDocumentsWorkingDocumentsWorkDocumentLine line = new SourceDocumentsWorkingDocumentsWorkDocumentLine();
                    line.Description = lineOld.Description;
                    line.Item = lineOld.Item;
                    line.ItemElementName = EnumParser.Parse<ItemChoiceType8>(lineOld.ItemElementName.ToString());
                    line.LineNumber = lineOld.LineNumber;
                    line.OrderReferences = GetOrderReferences(lineOld.OrderReferences);
                    line.ProductCode = lineOld.ProductCode;
                    line.ProductDescription = lineOld.ProductDescription;
                    line.Quantity = lineOld.Quantity;
                    line.SettlementAmount = lineOld.SettlementAmount;
                    line.SettlementAmountSpecified = lineOld.SettlementAmountSpecified;
                    line.Tax = GetTax(lineOld.Tax);
                    line.TaxExemptionReason = lineOld.TaxExemptionReason;
                    line.TaxPointDate = lineOld.TaxPointDate;
                    line.UnitOfMeasure = lineOld.UnitOfMeasure;
                    line.UnitPrice = lineOld.UnitPrice;

                    lines.Add(line);
                }
            }

            return lines.ToArray();
        }

        private static SourceDocumentsWorkingDocumentsWorkDocumentDocumentTotals GetWorkingDocumentDocumentTotals(Model.V2.SourceDocumentsWorkingDocumentsWorkDocumentDocumentTotals totalsOld)
        {
            SourceDocumentsWorkingDocumentsWorkDocumentDocumentTotals totals = null;
            if (totalsOld != null)
            {
                totals = new SourceDocumentsWorkingDocumentsWorkDocumentDocumentTotals();
                totals.Currency = GetCurrency(totalsOld.Currency);
                totals.GrossTotal = totalsOld.GrossTotal;
                totals.NetTotal = totalsOld.NetTotal;
                totals.TaxPayable = totalsOld.TaxPayable;
            }
            return totals;
        }

        private static SourceDocumentsWorkingDocumentsWorkDocumentDocumentStatus GetWorkingDocumentWorkStatus(Model.V2.SourceDocumentsWorkingDocumentsWorkDocumentDocumentStatus statusOld)
        {
            SourceDocumentsWorkingDocumentsWorkDocumentDocumentStatus status = null;
            if (statusOld != null)
            {
                status = new SourceDocumentsWorkingDocumentsWorkDocumentDocumentStatus();
                status.Reason = statusOld.Reason;
                status.SourceID = statusOld.SourceID;
                status.WorkStatus = EnumParser.Parse<WorkStatus>(statusOld.WorkStatus.ToString());
                status.WorkStatusDate = statusOld.WorkStatusDate;
            }
            return status;
        }
        #endregion WorkingDocuments

        #region MovementOfGoods
        private static SourceDocumentsMovementOfGoods GetMovementOfGoods(Model.V2.SourceDocumentsMovementOfGoods movementOfGoodsOld)
        {
            if (movementOfGoodsOld == null)
                return null;

            SourceDocumentsMovementOfGoods movementOfGoods = new SourceDocumentsMovementOfGoods();
            movementOfGoods.NumberOfMovementLines = movementOfGoodsOld.NumberOfMovementLines;
            movementOfGoods.TotalQuantityIssued = movementOfGoodsOld.TotalQuantityIssued;
            movementOfGoods.StockMovement = GetStockMovement(movementOfGoodsOld.StockMovement);

            return movementOfGoods;
        }

        private static SourceDocumentsMovementOfGoodsStockMovement[] GetStockMovement(Model.V2.SourceDocumentsMovementOfGoodsStockMovement[] stockMovementOld)
        {
            List<SourceDocumentsMovementOfGoodsStockMovement> stockMovement = new List<SourceDocumentsMovementOfGoodsStockMovement>();
            if (stockMovementOld != null && stockMovementOld.Length > 0)
            {
                foreach (var stockOld in stockMovementOld)
                {
                    SourceDocumentsMovementOfGoodsStockMovement stock = new SourceDocumentsMovementOfGoodsStockMovement();
                    stock.ATDocCodeID = stockOld.ATDocCodeID;
                    stock.DocumentNumber = stockOld.DocumentNumber;
                    stock.DocumentStatus = GetMovementOfGoodsStatus(stockOld.DocumentStatus);
                    stock.DocumentTotals = GetMovementOfGoodsTotals(stockOld.DocumentTotals);
                    stock.EACCode = stockOld.EACCode;
                    stock.Hash = stockOld.Hash;
                    stock.HashControl = stockOld.HashControl;
                    stock.Item = stockOld.Item;
                    stock.ItemElementName = EnumParser.Parse<ItemChoiceType6>(stockOld.ItemElementName.ToString());
                    stock.Line = GetMovementOfGoodsLines(stockOld.Line);
                    stock.MovementComments = stockOld.MovementComments;
                    stock.MovementDate = stockOld.MovementDate;
                    stock.MovementEndTime = stockOld.MovementEndTime;
                    stock.MovementEndTimeSpecified = stockOld.MovementEndTimeSpecified;
                    stock.MovementStartTime = stockOld.MovementStartTime;
                    stock.MovementType = EnumParser.Parse<MovementType>(stockOld.MovementType.ToString());
                    stock.Period = stockOld.Period;
                    stock.ShipFrom = GetShippingPoint(stockOld.ShipFrom);
                    stock.ShipTo = GetShippingPoint(stockOld.ShipTo);
                    stock.SourceID = stockOld.SourceID;
                    stock.SystemEntryDate = stockOld.SystemEntryDate;
                    stock.TransactionID = stockOld.TransactionID;

                    stockMovement.Add(stock);
                }
            }

            return stockMovement.ToArray();
        }

        private static SourceDocumentsMovementOfGoodsStockMovementLine[] GetMovementOfGoodsLines(Model.V2.SourceDocumentsMovementOfGoodsStockMovementLine[] linesOld)
        {
            if (linesOld == null || linesOld.Length <= 0)
                return null;

            List<SourceDocumentsMovementOfGoodsStockMovementLine> lines = new List<SourceDocumentsMovementOfGoodsStockMovementLine>();
            foreach (var lineOld in linesOld)
            {
                SourceDocumentsMovementOfGoodsStockMovementLine line = new SourceDocumentsMovementOfGoodsStockMovementLine();
                line.Description = lineOld.Description;
                line.Item = lineOld.Item;
                line.ItemElementName = EnumParser.Parse<ItemChoiceType7>(lineOld.ItemElementName.ToString());
                line.LineNumber = lineOld.LineNumber;
                line.OrderReferences = GetOrderReferences(lineOld.OrderReferences);
                line.ProductCode = lineOld.ProductCode;
                line.ProductDescription = lineOld.ProductDescription;
                line.Quantity = lineOld.Quantity;
                line.SettlementAmount = lineOld.SettlementAmount;
                line.SettlementAmountSpecified = lineOld.SettlementAmountSpecified;
                line.Tax = GetMovementTax(lineOld.Tax);
                line.TaxExemptionReason = lineOld.TaxExemptionReason;
                line.UnitOfMeasure = lineOld.UnitOfMeasure;
                line.UnitPrice = lineOld.UnitPrice;

                lines.Add(line);
            }

            return lines.ToArray();
        }

        private static MovementTax GetMovementTax(Model.V2.MovementTax taxOld)
        {
            MovementTax tax = new MovementTax();
            if (taxOld != null)
            {
                tax.TaxPercentage = taxOld.TaxPercentage;
                tax.TaxCode = taxOld.TaxCode;
                tax.TaxCountryRegion = taxOld.TaxCountryRegion;
                tax.TaxType = EnumParser.Parse<SAFTPTMovementTaxType>(taxOld.TaxType.ToString());
            }

            return tax;
        }

        private static SourceDocumentsMovementOfGoodsStockMovementDocumentTotals GetMovementOfGoodsTotals(Model.V2.SourceDocumentsMovementOfGoodsStockMovementDocumentTotals totalsOld)
        {
            if (totalsOld == null)
                return null;

            SourceDocumentsMovementOfGoodsStockMovementDocumentTotals totals = new SourceDocumentsMovementOfGoodsStockMovementDocumentTotals();
            totals.Currency = GetCurrency(totalsOld.Currency);
            totals.GrossTotal = totalsOld.GrossTotal;
            totals.NetTotal = totalsOld.NetTotal;
            totals.TaxPayable = totalsOld.TaxPayable;

            return totals;
        }

        private static SourceDocumentsMovementOfGoodsStockMovementDocumentStatus GetMovementOfGoodsStatus(Model.V2.SourceDocumentsMovementOfGoodsStockMovementDocumentStatus statusOld)
        {
            if (statusOld == null)
                return null;

            SourceDocumentsMovementOfGoodsStockMovementDocumentStatus status = new SourceDocumentsMovementOfGoodsStockMovementDocumentStatus();
            status.MovementStatus = EnumParser.Parse<MovementStatus>(statusOld.MovementStatus.ToString());
            status.MovementStatusDate = statusOld.MovementStatusDate;
            status.Reason = statusOld.Reason;
            status.SourceBilling = EnumParser.Parse<SAFTPTSourceBilling>(statusOld.SourceBilling.ToString());
            status.SourceID = statusOld.SourceID;

            return status;
        }
        #endregion MovementOfGoods

        #region SalesInvoices
        private static SourceDocumentsSalesInvoices GetSalesInvoices(Model.V2.SourceDocumentsSalesInvoices sourceDocumentsSalesInvoicesOld)
        {
            if (sourceDocumentsSalesInvoicesOld == null)
                return null;

            SourceDocumentsSalesInvoices sourceDocumentsSalesInvoices = new SourceDocumentsSalesInvoices();
            sourceDocumentsSalesInvoices.NumberOfEntries = sourceDocumentsSalesInvoicesOld.NumberOfEntries;
            sourceDocumentsSalesInvoices.TotalCredit = sourceDocumentsSalesInvoicesOld.TotalCredit;
            sourceDocumentsSalesInvoices.TotalDebit = sourceDocumentsSalesInvoicesOld.TotalDebit;
            sourceDocumentsSalesInvoices.Invoice = GetInvoices(sourceDocumentsSalesInvoicesOld.Invoice);

            return sourceDocumentsSalesInvoices;
        }

        private static SourceDocumentsSalesInvoicesInvoice[] GetInvoices(Model.V2.SourceDocumentsSalesInvoicesInvoice[] invoicesOld)
        {
            if (invoicesOld == null)
                return null;

            List<SourceDocumentsSalesInvoicesInvoice> invoices = new List<SourceDocumentsSalesInvoicesInvoice>();

            foreach (var invoiceOld in invoicesOld)
            {
                SourceDocumentsSalesInvoicesInvoice invoice = new SourceDocumentsSalesInvoicesInvoice();
                invoice.CustomerID = invoiceOld.CustomerID;
                invoice.SourceID = invoiceOld.SourceID;
                invoice.DocumentStatus = GetInvoiceStatus(invoiceOld.DocumentStatus);
                invoice.DocumentTotals = GetInvoiceTotals(invoiceOld.DocumentTotals);
                invoice.Hash = invoiceOld.Hash;
                invoice.HashControl = invoiceOld.HashControl;
                invoice.InvoiceDate = invoiceOld.InvoiceDate;
                invoice.InvoiceNo = invoiceOld.InvoiceNo;
                invoice.InvoiceType = EnumParser.Parse<InvoiceType>(invoiceOld.InvoiceType.ToString());
                invoice.Line = GetInvoiceLines(invoiceOld.Line);
                invoice.Period = invoiceOld.Period;
                invoice.SpecialRegimes = new SpecialRegimes();
                invoice.SpecialRegimes.SelfBillingIndicator = invoiceOld.SelfBillingIndicator;
                invoice.ShipFrom = GetShippingPoint(invoiceOld.ShipFrom);
                invoice.ShipTo = GetShippingPoint(invoiceOld.ShipTo);
                invoice.SystemEntryDate = invoiceOld.SystemEntryDate;
                invoice.TransactionID = invoiceOld.TransactionID;

                invoices.Add(invoice);
            }

            return invoices.ToArray();
        }

        private static SourceDocumentsSalesInvoicesInvoiceLine[] GetInvoiceLines(Model.V2.SourceDocumentsSalesInvoicesInvoiceLine[] invoiceLinesOld)
        {
            if (invoiceLinesOld == null)
                return null;

            List<SourceDocumentsSalesInvoicesInvoiceLine> invoiceLines = new List<SourceDocumentsSalesInvoicesInvoiceLine>();

            foreach (var lineOld in invoiceLinesOld)
            {
                SourceDocumentsSalesInvoicesInvoiceLine line = new SourceDocumentsSalesInvoicesInvoiceLine();
                line.Description = lineOld.Description;
                line.Item = lineOld.Item;
                line.ItemElementName = EnumParser.Parse<ItemChoiceType5>(lineOld.ItemElementName.ToString());
                line.LineNumber = lineOld.LineNumber;
                line.OrderReferences = GetOrderReferences(lineOld.OrderReferences);
                line.ProductCode = lineOld.ProductCode;
                line.ProductDescription = lineOld.ProductDescription;
                line.Quantity = lineOld.Quantity;
                line.References = GetLineReferences(lineOld.References);
                line.SettlementAmount = lineOld.SettlementAmount;
                line.SettlementAmountSpecified = lineOld.SettlementAmountSpecified;
                line.Tax = GetTax(lineOld.Tax);
                line.TaxExemptionReason = lineOld.TaxExemptionReason;
                line.TaxPointDate = lineOld.TaxPointDate;
                line.UnitOfMeasure = lineOld.UnitOfMeasure;
                line.UnitPrice = lineOld.UnitPrice;

                invoiceLines.Add(line);
            }

            return invoiceLines.ToArray();
        }

        private static SourceDocumentsSalesInvoicesInvoiceDocumentTotals GetInvoiceTotals(Model.V2.SourceDocumentsSalesInvoicesInvoiceDocumentTotals documentTotalsOld)
        {
            if (documentTotalsOld == null)
                return null;

            SourceDocumentsSalesInvoicesInvoiceDocumentTotals documentTotals = new SourceDocumentsSalesInvoicesInvoiceDocumentTotals();
            documentTotals.Currency = GetCurrency(documentTotalsOld.Currency);
            documentTotals.GrossTotal = documentTotalsOld.GrossTotal;
            documentTotals.NetTotal = documentTotalsOld.NetTotal;
            if (documentTotalsOld.Settlement != null && documentTotalsOld.Settlement.Length > 0)
            {
                List<Settlement> listSettlement = new List<Settlement>();
                foreach (var settlement in documentTotalsOld.Settlement)
                {
                    listSettlement.Add(new Settlement
                    {
                        SettlementAmount = settlement.SettlementAmount,
                        SettlementAmountSpecified = settlement.SettlementAmountSpecified,
                        SettlementDate = settlement.SettlementDate,
                        SettlementDateSpecified = settlement.SettlementDateSpecified,
                        SettlementDiscount = settlement.SettlementDiscount
                    });
                }
                documentTotals.Settlement = listSettlement.ToArray();
            }
            documentTotals.TaxPayable = documentTotalsOld.TaxPayable;

            return documentTotals;
        }

        private static SourceDocumentsSalesInvoicesInvoiceDocumentStatus GetInvoiceStatus(Model.V2.SourceDocumentsSalesInvoicesInvoiceDocumentStatus documentStatusOld)
        {
            if (documentStatusOld == null)
                return null;
            
            SourceDocumentsSalesInvoicesInvoiceDocumentStatus documentStatus = new SourceDocumentsSalesInvoicesInvoiceDocumentStatus();
            documentStatus.InvoiceStatus = EnumParser.Parse<InvoiceStatus>(documentStatusOld.InvoiceStatus.ToString());
            documentStatus.InvoiceStatusDate = documentStatusOld.InvoiceStatusDate;
            documentStatus.Reason = documentStatusOld.Reason;
            documentStatus.SourceID = documentStatusOld.SourceID;
            documentStatus.SourceBilling = EnumParser.Parse<SAFTPTSourceBilling>(documentStatusOld.SourceBilling.ToString());

            return documentStatus;
        }
        #endregion SalesInvoices

        #endregion SourceDocuments

        #region GeneralLedger
        private static GeneralLedgerEntries GetGeneralLedgerEntries(Model.V2.GeneralLedgerEntries generalLedgerEntriesOld)
        {
            if (generalLedgerEntriesOld == null)
                return null;

            GeneralLedgerEntries generalLedgerEntries = new GeneralLedgerEntries();
            generalLedgerEntries.NumberOfEntries = generalLedgerEntriesOld.NumberOfEntries;
            generalLedgerEntries.TotalCredit = generalLedgerEntriesOld.TotalCredit;
            generalLedgerEntries.TotalDebit = generalLedgerEntriesOld.TotalDebit;
            generalLedgerEntries.Journal = GetJornal(generalLedgerEntriesOld.Journal);

            return generalLedgerEntries;
        }

        private static GeneralLedgerEntriesJournal[] GetJornal(Model.V2.GeneralLedgerEntriesJournal[] generalLedgerEntriesJournalOld)
        {
            if (generalLedgerEntriesJournalOld == null)
                return null;

            List<GeneralLedgerEntriesJournal> generalLedgerEntriesJournal = new List<GeneralLedgerEntriesJournal>();

            foreach (var entryJornalOld in generalLedgerEntriesJournalOld)
            {
                GeneralLedgerEntriesJournal entryJournal = new GeneralLedgerEntriesJournal();
                entryJournal.Description = entryJornalOld.Description;
                entryJournal.JournalID = entryJornalOld.JournalID;
                entryJournal.Transaction = GetTransaction(entryJornalOld.Transaction);

                generalLedgerEntriesJournal.Add(entryJournal);
            }

            return generalLedgerEntriesJournal.ToArray();
        }

        private static GeneralLedgerEntriesJournalTransaction[] GetTransaction(Model.V2.GeneralLedgerEntriesJournalTransaction[] generalLedgerEntriesJournalTransactionOld)
        {
            if (generalLedgerEntriesJournalTransactionOld == null)
                return null;

            List<GeneralLedgerEntriesJournalTransaction> generalLedgerEntriesJournalTransaction = new List<GeneralLedgerEntriesJournalTransaction>();

            foreach (var entryJornalTransactionOld in generalLedgerEntriesJournalTransactionOld)
            {
                GeneralLedgerEntriesJournalTransaction entryJornalTransaction = new GeneralLedgerEntriesJournalTransaction();
                entryJornalTransaction.Description = entryJornalTransactionOld.Description;
                entryJornalTransaction.DocArchivalNumber = entryJornalTransactionOld.DocArchivalNumber;
                entryJornalTransaction.GLPostingDate = entryJornalTransactionOld.GLPostingDate;
                entryJornalTransaction.Line = GetJornalTransactionLine(entryJornalTransactionOld.Line);
                entryJornalTransaction.Period = entryJornalTransactionOld.Period;
                entryJornalTransaction.SourceID = entryJornalTransactionOld.SourceID;
                entryJornalTransaction.TransactionDate = entryJornalTransactionOld.TransactionDate;
                entryJornalTransaction.TransactionID = entryJornalTransactionOld.TransactionID;
                entryJornalTransaction.TransactionType = EnumParser.Parse<TransactionType>(entryJornalTransactionOld.TransactionType.ToString());

                generalLedgerEntriesJournalTransaction.Add(entryJornalTransaction);
            }

            return generalLedgerEntriesJournalTransaction.ToArray();
        }

        private static GeneralLedgerEntriesJournalTransactionLine[] GetJornalTransactionLine(Model.V2.GeneralLedgerEntriesJournalTransactionLine[] entriesJournalTransactionLinesOld)
        {
            if (entriesJournalTransactionLinesOld == null)
                return null;

            List<GeneralLedgerEntriesJournalTransactionLine> entriesJournalTransactionLines = new List<GeneralLedgerEntriesJournalTransactionLine>();

            foreach (var entriesJournalTransactionLineOld in entriesJournalTransactionLinesOld)
            {
                GeneralLedgerEntriesJournalTransactionLine entriesJournalTransactionLine = new GeneralLedgerEntriesJournalTransactionLine();
                entriesJournalTransactionLine.AccountID = entriesJournalTransactionLineOld.AccountID;
                entriesJournalTransactionLine.Description = entriesJournalTransactionLineOld.Description;
                entriesJournalTransactionLine.Item = entriesJournalTransactionLineOld.Item;
                entriesJournalTransactionLine.ItemElementName = EnumParser.Parse<ItemChoiceType4>(entriesJournalTransactionLineOld.ItemElementName.ToString());
                entriesJournalTransactionLine.RecordID = entriesJournalTransactionLineOld.RecordID;
                entriesJournalTransactionLine.SourceDocumentID = entriesJournalTransactionLineOld.SourceDocumentID;
                entriesJournalTransactionLine.SystemEntryDate = entriesJournalTransactionLineOld.SystemEntryDate;


                entriesJournalTransactionLines.Add(entriesJournalTransactionLine);
            }

            return entriesJournalTransactionLines.ToArray();
        }
        #endregion GeneralLedger

        #region Header
        static Header GetHeader(Model.V2.Header oldHeader)
        {
            if (oldHeader == null)
                return null;

            Header header = new Header();
            header.AuditFileVersion = oldHeader.AuditFileVersion;
            header.BusinessName = oldHeader.BusinessName;
            if (oldHeader.CompanyAddress != null)
            {
                header.CompanyAddress = new AddressStructurePT();
                header.CompanyAddress.AddressDetail = oldHeader.CompanyAddress.AddressDetail;
                header.CompanyAddress.BuildingNumber = oldHeader.CompanyAddress.BuildingNumber;
                header.CompanyAddress.City = oldHeader.CompanyAddress.City;
                header.CompanyAddress.Country = oldHeader.CompanyAddress.Country.ToString();
                header.CompanyAddress.PostalCode = oldHeader.CompanyAddress.PostalCode;
                header.CompanyAddress.Region = oldHeader.CompanyAddress.Region;
                header.CompanyAddress.StreetName = oldHeader.CompanyAddress.StreetName;
            }
            header.CompanyID = oldHeader.CompanyID;
            header.CompanyName = oldHeader.CompanyName;
            header.CurrencyCode = oldHeader.CurrencyCode;
            header.DateCreated = oldHeader.DateCreated;
            header.Email = oldHeader.Email;
            header.EndDate = oldHeader.EndDate;
            header.Fax = oldHeader.Fax;
            header.FiscalYear = oldHeader.FiscalYear;
            header.HeaderComment = oldHeader.HeaderComment;
            header.ProductCompanyTaxID = oldHeader.ProductCompanyTaxID;
            header.ProductID = oldHeader.ProductID;
            header.ProductVersion = oldHeader.ProductVersion;
            header.SoftwareCertificateNumber = oldHeader.SoftwareCertificateNumber;
            header.StartDate = oldHeader.StartDate;
            TaxAccountingBasis taxAccountingBasis;
            Enum.TryParse<TaxAccountingBasis>(oldHeader.TaxAccountingBasis.ToString(), true, out taxAccountingBasis);
            header.TaxAccountingBasis = taxAccountingBasis;
            header.TaxEntity = oldHeader.TaxEntity;
            header.TaxRegistrationNumber = oldHeader.TaxRegistrationNumber;
            header.Telephone = oldHeader.Telephone;
            header.Website = oldHeader.Website;

            return header;
        }
        #endregion Header

        #region MasterFiles
        static AuditFileMasterFiles GetMastesFiles(Model.V2.AuditFileMasterFiles masterFilesOld)
        {
            if (masterFilesOld == null)
                return null;

            AuditFileMasterFiles masterFiles = new AuditFileMasterFiles();
            masterFiles.Customer = GetCustomers(masterFilesOld.Customer);
            masterFiles.Product = GetProducts(masterFilesOld.Product);
            masterFiles.TaxTable = GetTaxTable(masterFilesOld.TaxTable);
            masterFiles.Supplier = GetSupplier(masterFilesOld.Supplier);
            masterFiles.GeneralLedger = GetGeneralLedger(masterFilesOld.GeneralLedger);

            return masterFiles;
        }

        private static GeneralLedger[] GetGeneralLedger(Model.V2.GeneralLedger[] generalLedgersOld)
        {
            if (generalLedgersOld == null)
                return null;

            List<GeneralLedger> generalLedgers = new List<GeneralLedger>();

            foreach (Model.V2.GeneralLedger generalLedgerOld in generalLedgersOld)
            {
                GeneralLedger generalLedger = new GeneralLedger();
                generalLedger.AccountDescription = generalLedgerOld.AccountDescription;
                generalLedger.AccountID = generalLedgerOld.AccountID;
                generalLedger.OpeningCreditBalance = generalLedgerOld.OpeningCreditBalance;
                generalLedger.OpeningDebitBalance = generalLedgerOld.OpeningDebitBalance;

                generalLedgers.Add(generalLedger);
            }

            return generalLedgers.ToArray();
        }

        private static Supplier[] GetSupplier(Model.V2.Supplier[] suppliersOld)
        {
            if (suppliersOld == null)
                return null;

            List<Supplier> suppliers = new List<Supplier>();

            foreach (Model.V2.Supplier supplierOld in suppliersOld)
            {
                Supplier supplier = new Supplier();
                supplier.AccountID = supplierOld.AccountID;
                if (supplierOld.BillingAddress != null)
                {
                    supplier.BillingAddress = new SupplierAddressStructure();
                    supplier.BillingAddress.AddressDetail = supplierOld.BillingAddress.AddressDetail;
                    supplier.BillingAddress.BuildingNumber = supplierOld.BillingAddress.BuildingNumber;
                    supplier.BillingAddress.City = supplierOld.BillingAddress.City;
                    supplier.BillingAddress.Country = supplierOld.BillingAddress.Country;
                    supplier.BillingAddress.PostalCode = supplierOld.BillingAddress.PostalCode;
                    supplier.BillingAddress.Region = supplierOld.BillingAddress.Region;
                    supplier.BillingAddress.StreetName = supplierOld.BillingAddress.StreetName;
                }
                supplier.CompanyName = supplierOld.CompanyName;
                supplier.Contact = supplierOld.Contact;
                supplier.Email = supplierOld.Email;
                supplier.Fax = supplierOld.Fax;
                supplier.SelfBillingIndicator = supplierOld.SelfBillingIndicator;
                if (supplierOld.ShipFromAddress != null && supplierOld.ShipFromAddress.Length > 0)
                {
                    List<SupplierAddressStructure> listShipFromAddress = new List<SupplierAddressStructure>();
                    foreach (var address in supplierOld.ShipFromAddress)
                    {
                        listShipFromAddress.Add(new SupplierAddressStructure
                        {
                            AddressDetail = address.AddressDetail,
                            BuildingNumber = address.BuildingNumber,
                            City = address.City,
                            Country = address.Country,
                            PostalCode = address.PostalCode,
                            Region = address.Region,
                            StreetName = address.StreetName
                        });
                    }
                    supplier.ShipFromAddress = listShipFromAddress.ToArray();
                }
                supplier.SupplierID = supplierOld.SupplierID;
                supplier.SupplierTaxID = supplierOld.SupplierTaxID;
                supplier.Telephone = supplierOld.Telephone;
                supplier.Website = supplierOld.Website;

                suppliers.Add(supplier);
            }

            return suppliers.ToArray();
        }

        private static TaxTableEntry[] GetTaxTable(Model.V2.TaxTableEntry[] taxesOld)
        {
            if (taxesOld == null)
                return null;

            List<TaxTableEntry> taxes = new List<TaxTableEntry>();

            if (taxesOld.Count() > 0)
            {
                foreach (Model.V2.TaxTableEntry taxOld in taxesOld)
                {
                    TaxTableEntry tax = new TaxTableEntry();
                    tax.Description = taxOld.Description;
                    tax.Item = taxOld.Item;
                    tax.ItemElementName = EnumParser.Parse<ItemChoiceType2>(taxOld.ItemElementName.ToString());
                    tax.TaxCode = taxOld.TaxCode;
                    tax.TaxCountryRegion = taxOld.TaxCountryRegion;
                    tax.TaxExpirationDate = taxOld.TaxExpirationDate;
                    tax.TaxExpirationDateSpecified = taxOld.TaxExpirationDateSpecified;
                    tax.TaxType = EnumParser.Parse<TaxType>(taxOld.TaxType.ToString());

                    taxes.Add(tax);
                }
            }
            return taxes.ToArray();
        }

        private static Product[] GetProducts(Model.V2.Product[] productsOld)
        {
            if (productsOld == null)
                return null;

            List<Product> produtos = new List<Product>();

            foreach (Model.V2.Product productOld in productsOld)
            {
                Product product = new Product();
                product.ProductCode = productOld.ProductCode;
                product.ProductDescription = productOld.ProductDescription;
                product.ProductGroup = productOld.ProductGroup;
                product.ProductNumberCode = productOld.ProductNumberCode;
                product.ProductType = EnumParser.Parse<ProductType>(productOld.ProductType.ToString());

                produtos.Add(product);
            }

            return produtos.ToArray();
        }

        private static Customer[] GetCustomers(Model.V2.Customer[] customersOld)
        {
            if (customersOld == null)
                return null;

            List<Customer> customers = new List<Customer>();

            Customer customer = null;
            foreach (Model.V2.Customer customerOld in customersOld)
            {
                customer = new Customer();
                customer.AccountID = customerOld.AccountID;
                if (customerOld.BillingAddress != null)
                {
                    customer.BillingAddress = new AddressStructure();
                    customer.BillingAddress.AddressDetail = customerOld.BillingAddress.AddressDetail;
                    customer.BillingAddress.BuildingNumber = customerOld.BillingAddress.BuildingNumber;
                    customer.BillingAddress.City = customerOld.BillingAddress.City;
                    customer.BillingAddress.Country = customerOld.BillingAddress.Country;
                    customer.BillingAddress.PostalCode = customerOld.BillingAddress.PostalCode;
                    customer.BillingAddress.Region = customerOld.BillingAddress.Region;
                    customer.BillingAddress.StreetName = customerOld.BillingAddress.StreetName;
                }
                customer.CompanyName = customerOld.CompanyName;
                customer.Contact = customerOld.Contact;
                customer.CustomerID = customerOld.CustomerID;
                customer.CustomerTaxID = customerOld.CustomerTaxID;
                customer.Email = customerOld.Email;
                customer.Fax = customerOld.Fax;
                customer.SelfBillingIndicator = customerOld.SelfBillingIndicator;
                if (customerOld.ShipToAddress != null && customerOld.ShipToAddress.Length > 0)
                {
                    List<AddressStructure> listAddressStructure = new List<AddressStructure>();
                    foreach (var shipToAddress in customerOld.ShipToAddress)
                    {
                        listAddressStructure.Add(new AddressStructure
                        {
                            AddressDetail = shipToAddress.AddressDetail,
                            BuildingNumber = shipToAddress.BuildingNumber,
                            City = shipToAddress.City,
                            Country = shipToAddress.Country,
                            PostalCode = shipToAddress.PostalCode,
                            Region = shipToAddress.Region,
                            StreetName = shipToAddress.StreetName
                        });
                    }
                    customer.ShipToAddress = listAddressStructure.ToArray();
                }
                customer.Telephone = customerOld.Telephone;
                customer.Website = customerOld.Website;

                customers.Add(customer);
            }

            return customers.ToArray();
        }
        #endregion MasterFiles

        private static Currency GetCurrency(Model.V2.Currency currencyOld)
        {
            if (currencyOld == null)
                return null;

            Currency currency = new Currency();
            currency.CurrencyAmount = currencyOld.CurrencyAmount;
            currency.CurrencyCode = currencyOld.CurrencyCode;
            currency.ExchangeRate = currencyOld.ExchangeRate;
            currency.ExchangeRateSpecified = currencyOld.ExchangeRateSpecified;

            return currency;
        }

        private static OrderReferences[] GetOrderReferences(Model.V2.OrderReferences[] orderReferencesOld)
        {
            if (orderReferencesOld == null)
                return null;

            List<OrderReferences> orderReferences = new List<OrderReferences>();

            foreach (var orderRefOld in orderReferencesOld)
            {
                OrderReferences orderRef = new OrderReferences();
                orderRef.OrderDate = orderRefOld.OrderDate;
                orderRef.OrderDateSpecified = orderRefOld.OrderDateSpecified;
                orderRef.OriginatingON = orderRefOld.OriginatingON;

                orderReferences.Add(orderRef);
            }

            return orderReferences.ToArray();
        }

        private static References[] GetLineReferences(Model.V2.References[] referencesOld)
        {
            if (referencesOld == null || referencesOld.Length == 0)
                return null;

            List<References> references = new List<References>();
            foreach (var referenceOld in referencesOld)
            {
                references.Add(new References
                {
                    Reason = referenceOld.Reason,
                    Reference = referenceOld.Reference
                });
            }

            return references.ToArray();
        }

        private static ShippingPointStructure GetShippingPoint(Model.V2.ShippingPointStructure shipFromOld)
        {
            if (shipFromOld == null)
                return null;

            ShippingPointStructure shipFrom = new ShippingPointStructure();
            shipFrom.DeliveryDate = shipFromOld.DeliveryDate;
            shipFrom.DeliveryDateSpecified = shipFromOld.DeliveryDateSpecified;
            shipFrom.DeliveryID = shipFromOld.DeliveryID;
            if (shipFromOld.Address != null)
            {
                shipFrom.Address = new AddressStructure();
                shipFrom.Address.AddressDetail = shipFromOld.Address.AddressDetail;
                shipFrom.Address.BuildingNumber = shipFromOld.Address.BuildingNumber;
                shipFrom.Address.City = shipFromOld.Address.City;
                shipFrom.Address.Country = shipFromOld.Address.Country;
                shipFrom.Address.PostalCode = shipFromOld.Address.PostalCode;
                shipFrom.Address.Region = shipFromOld.Address.Region;
                shipFrom.Address.StreetName = shipFromOld.Address.StreetName;
            }
            return shipFrom;
        }

        private static Tax GetTax(Model.V2.Tax taxOld)
        {
            Tax tax = new Tax();
            if (taxOld != null)
            {
                tax.Item = taxOld.Item;
                tax.ItemElementName = EnumParser.Parse<ItemChoiceType1>(taxOld.ItemElementName.ToString());
                tax.TaxCode = taxOld.TaxCode;
                tax.TaxCountryRegion = taxOld.TaxCountryRegion;
                tax.TaxType = EnumParser.Parse<TaxType>(taxOld.TaxType.ToString());
            }

            return tax;
        }
    }
}
